package com.arcbbs.controller;

import com.arcbbs.config.WebConfig;
import com.arcbbs.constant.BaseConstant;
import com.arcbbs.entity.SessionAdminDto;
import com.arcbbs.entity.SessionWebUserDto;
import com.arcbbs.entity.UserInfo;
import com.arcbbs.exception.BaseException;
import com.arcbbs.mapper.UserInfoMapper;
import com.arcbbs.result.PageResult;
import com.arcbbs.result.Result;
import com.arcbbs.service.UserInfoService;
import com.arcbbs.utils.CreateImageCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/admin")
public class AccountController {
    @Resource
    private WebConfig webConfig;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserInfoService userInfoService;
    @GetMapping("/checkcode")
    public void checkcode(HttpServletResponse response, HttpSession session, Integer type) throws IOException {
        CreateImageCode vcode = new CreateImageCode(130, 38, 4);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        //生效时间
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        String code = vcode.getCode();

        if (type == null || type == 0) {
            //登录注册
            session.setAttribute(BaseConstant.CHECK_CODE_KEY, code);
            System.out.println(code + "注册");
        } else {
            session.setAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL, code);
            System.out.println(code + "邮箱");

        }
        System.out.println((String) session.getAttribute(BaseConstant.CHECK_CODE_KEY_EMAIL));

        vcode.write(response.getOutputStream());

    }
    @RequestMapping("/login")
    public Result login(HttpSession httpSession,String username,String password,String checkcode){
        System.out.println(webConfig.getAccount());
        System.out.println(webConfig.getPassword());
        if(!webConfig.getAccount().equals(username) ||!webConfig.getPassword().equals(password)){
            throw new BaseException("账号密码错误");
        }
        if(!checkcode.equalsIgnoreCase((String) httpSession.getAttribute(BaseConstant.CHECK_CODE_KEY))){
            throw new BaseException("验证码错误");
        }
        SessionAdminDto sessionAdminDto=new SessionAdminDto();
        sessionAdminDto.setAccount(username);
        httpSession.setAttribute("sessionKey",sessionAdminDto);
        return Result.success(sessionAdminDto);

    }
    @RequestMapping("/userlist")
    public Result userlist(Integer pageNo){
        PageResult list=userInfoService.selectbypage(pageNo);
        return Result.success(list);

    }
    @RequestMapping("/disableuser")
    public Result disableuser(String userId,Integer status){
        UserInfo userInfo=userInfoService.selectbyuserid(userId);
        if(userInfo == null){
            throw new BaseException("用户不存在");
        }
        userInfo.setStatus(status);
        userInfoService.updatebyuserid(userInfo);
        return Result.success();

    }
}
